package com.yh.csx.bsf.flow.engine;

import com.yh.csx.bsf.core.util.StringUtils;
import com.yh.csx.bsf.flow.core.base.Model;
import com.yh.csx.bsf.flow.core.base.Paint;
import com.yh.csx.bsf.flow.engine.api.CommonApi;
import lombok.var;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: chejiangyi
 * @version: 2019-11-06 14:01
 * 流程图绘制
 **/
public class PaintProvider {
    public Paint paint(List<Model.Node> nodes){
        Paint p = new Paint();
        p.setLinks( new ArrayList<>());
        p.setNodes( new ArrayList<>());
        nodes.forEach(c->{
            var n = new Paint.node();
            n.setKey(c.getTitle());
            n.setName(c.getTitle());
            if("开始".equals(c.getTitle())||"结束".equals(c.getTitle())){
                n.setCategory("starttemplate");
            }
            else if(c.getTypeEnum() == Model.TypeEnum.Auto){
                n.setCategory("autotemplate");
            }
            n.setLoc(c.getLocation());
            p.getNodes().add(n);
        });
        nodes.forEach(c->{
            var tos = CommonApi.routeToNodes(c.getRoutes());
            if(tos!=null){
                tos.forEach(t->{
                    if(!StringUtils.isEmpty(t.getNode())) {
                        var rnode = t.getNode().trim();
                        var f = nodes.stream().filter(o->o.getTitle().equals(rnode)).findFirst();
                        if(f.isPresent()) {
                            var l = new Paint.link();
                            l.setFrom(c.getTitle());
                            l.setTo(rnode);
                            l.setText(t.getTitle());
                            p.getLinks().add(l);
                        }
                    }
                });
            }
        });
        return p;
    }
}
